package com.winning.vsts.merge.service.domain;

import com.alibaba.fastjson.JSONObject;
import com.winning.vsts.merge.service.entity.PullRequestDO;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;

/**
 * (PullRequest)实体类
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/17
 */
@DynamicInsert
@Entity(name = "pull_request")
public class PullRequest implements Serializable {
    private static final long serialVersionUID = 477718247852293800L;
    /**
     * 请求ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    /**
     * vsts 请求id
     */
    private String vstsId;
    /**
     * 项目ID
     */
    private Integer projectId;
    /**
     * 项目名称
     */
    private String projectName;
    /**
     * 每次合并的批次号
     */
    private String pullNo;
    /**
     * 请求状态
     */
    private String status;
    /**
     * 合并信息
     */
    private String mergeStatus;
    /**
     * commit 数量
     */
    private Integer commitCount;
    /**
     * PR请求数量
     */
    private Integer prCommitCount;
    /**
     * 源分支
     */
    private String sourceBranch;
    /**
     * 目标分支
     */
    private String targetBranch;
    /**
     * PR请求URL
     */
    private String requestUrl;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 修改时间
     */
    private Date updateTime;


    public static PullRequest createPullRequest(PullRequestDO requestDO) {
        PullRequest pullRequest = new PullRequest();
        pullRequest.setPullNo(requestDO.getPullNo());
        VstsProject vstsProject = requestDO.getVstsProject();
        pullRequest.setProjectId(vstsProject.getProjectId());
        pullRequest.setProjectName(vstsProject.getProjectName());
        pullRequest.setCommitCount(requestDO.getCommitCount());
        pullRequest.setPrCommitCount(requestDO.getPrCommitCount());
        pullRequest.setSourceBranch(requestDO.getSourceBranch());
        pullRequest.setTargetBranch(requestDO.getTargetBranch());
        pullRequest.setStatus("0");
        pullRequest.setMergeStatus("0");
        return pullRequest;
    }


    public static PullRequest createPullRequest(Integer requestId, JSONObject jsonObject) {
        PullRequest pullRequest = new PullRequest();
        pullRequest.setId(requestId);
        String status = jsonObject.getString("status");
        String mergeStatus = jsonObject.getString("mergeStatus");
        String pullRequestId = jsonObject.getString("pullRequestId");
        JSONObject repository = jsonObject.getJSONObject("repository");
        String remoteUrl = repository.getString("remoteUrl");
        pullRequest.setStatus(status);
        // 队列正在生成的话会变成这个状态
        pullRequest.setMergeStatus(StringUtils.defaultString(mergeStatus, "actived"));
        pullRequest.setVstsId(pullRequestId);
        pullRequest.setRequestUrl(String.format("%s/pullrequest/%s?_a=overview", remoteUrl, pullRequestId));
        pullRequest.setUpdateTime(new Date());
        return pullRequest;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getVstsId() {
        return vstsId;
    }

    public void setVstsId(String vstsId) {
        this.vstsId = vstsId;
    }

    public Integer getProjectId() {
        return projectId;
    }

    public void setProjectId(Integer projectId) {
        this.projectId = projectId;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getPullNo() {
        return pullNo;
    }

    public void setPullNo(String pullNo) {
        this.pullNo = pullNo;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMergeStatus() {
        return mergeStatus;
    }

    public void setMergeStatus(String mergeStatus) {
        this.mergeStatus = mergeStatus;
    }

    public Integer getCommitCount() {
        return commitCount;
    }

    public void setCommitCount(Integer commitCount) {
        this.commitCount = commitCount;
    }

    public Integer getPrCommitCount() {
        return prCommitCount;
    }

    public void setPrCommitCount(Integer prCommitCount) {
        this.prCommitCount = prCommitCount;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getSourceBranch() {
        return sourceBranch;
    }

    public void setSourceBranch(String sourceBranch) {
        this.sourceBranch = sourceBranch;
    }

    public String getTargetBranch() {
        return targetBranch;
    }

    public void setTargetBranch(String targetBranch) {
        this.targetBranch = targetBranch;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
}